package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeKeyC;
import com.my.zl.console.common.constant.AutocodeRespE;
import com.my.zl.console.dao.autocode.ZlEntityAttrMapper;
import com.my.zl.console.dao.autocode.ZlEntityMapper;
import com.my.zl.console.entity.autocode.ZlEntity;
import com.my.zl.console.po.autocode.entity.ZlEntityVo;
import com.my.zl.console.dto.autocode.entity.ReqEntityAddDTO;
import com.my.zl.console.dto.autocode.entity.ReqEntityDelDTO;
import com.my.zl.console.dto.autocode.entity.ReqEntityEditDTO;
import com.my.zl.console.dto.autocode.entity.ReqEntityQueryDTO;
import com.my.zl.console.dto.autocode.entity.ReqEntityDetailDTO;
import com.my.zl.console.dto.autocode.entity.ReqEntityStateDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityAddDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityDelDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityEditDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityQueryDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityDetailDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityStateDTO;
import com.my.zl.console.dto.autocode.entity.ResEntityVO;
import com.my.zl.console.dto.autocode.entity.ResEntityVO2;
import com.my.zl.console.service.autocode.IEntityService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 实体配置
 * @author ZL
 */
@Service
public class EntityServiceImpl extends BaseManager implements IEntityService {

    @Autowired
    private ZlEntityMapper entityMapper;
    @Autowired
    private ZlEntityAttrMapper entityAttrMapper;

    @Override
    public ResEntityQueryDTO queryEntityList(ReqEntityQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResEntityQueryDTO resDTO = BeanUtil.copyProperties(page, ResEntityQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlEntityVo> list = entityMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResEntityVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResEntityDetailDTO queryEntityDetail(ReqEntityDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResEntityDetailDTO res = new ResEntityDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlEntity view = entityMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResEntityAddDTO insertEntityInfo(ReqDTO<ReqEntityAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityAddDTO reqData = reqInfo.getReqData();
        ResEntityAddDTO res = new ResEntityAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlEntity record = new ZlEntity();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setEntityId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityId",record.getEntityId());
        //检查主键是否冲突
        if(entityMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        entityMapper.insertSelective(record);
        
        ZlEntity keyInfo = entityMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResEntityEditDTO updateEntityInfo(ReqDTO<ReqEntityEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityEditDTO reqData = reqInfo.getReqData();
        ResEntityEditDTO res = new ResEntityEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlEntity record = new ZlEntity();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        entityMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityId",record.getEntityId());
        ZlEntity keyInfo = entityMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResEntityDelDTO deleteEntityInfo(ReqDTO<ReqEntityDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityDelDTO reqData = reqInfo.getReqData();
        ResEntityDelDTO res = new ResEntityDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityId",reqData.getEntityId());
        if(entityAttrMapper.countExistKey(paramMap) > 0){
            throw new BizException(AutocodeRespE.DELETE_NOT_ALLOWED);
        }
        
        paramMap = BeanUtil.beanToMap(reqData);
        entityMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResEntityStateDTO updateEntityState(ReqDTO<ReqEntityStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityStateDTO reqData = reqInfo.getReqData();
        ResEntityStateDTO res = new ResEntityStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlEntity record = new ZlEntity();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        entityMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityId",record.getEntityId());
        ZlEntity keyInfo = entityMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}